package com.geekaca.web.service;

import com.geekaca.web.mapper.BrandMapper;
import com.geekaca.web.pojo.Brand;
import com.geekaca.web.utils.SqlSessionFactoryUtils;
import com.geekaca.web.utils.SqlUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandService {
//    public List<Brand> getAllBrands(int pNO, int pageSize) {
//        SqlSession sqlSession = SqlUtil.openSession();
//        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        List<Brand> brands = brandMapper.selectAllBrands();
//        sqlSession.close();
//        return brands;
//    }

    public List<Brand> getBrandsByPage(Integer page) {

        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int start = (page - 1) * 5;
        int end = 5;
        List<Brand> brands = brandMapper.selectBrandByPageNew(start, end);
        return brands;
    }

    public int addBrand(Brand brand) {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int i = brandMapper.insertBrand(brand);
        sqlSession.close();
        return i;
    }

    public int updateBrand(Brand brand) {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int i = brandMapper.updateBrand(brand);
        return i;
    }

    public int deleteBrand(Integer id) {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int i = brandMapper.deleteBrand(id);
        return i;
    }

    //    public List<Brand> selectAll() {
//        SqlSession sqlSession = SqlUtil.openSession();
//        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//        List<Brand> brands = brandMapper.selectAllBrands();
//        sqlSession.close();
//        return brands;
//    }
    public Integer getAllBrandsCount(Brand brand, Integer pageNo, Integer pageSize) {
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        Integer count = brandMapper.selectAllCount(brand, pageNo, pageSize);
        sqlSession.close();
        return count;
    }

    public List<Brand> selectAll(Brand brand, Integer pageNo, Integer pageSize) {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        // limit 的第一个参数
        int start = (pageNo - 1) * pageSize;
        List<Brand> brands = brandMapper.selectAllBrands(brand, start, pageSize);
        sqlSession.close();
        return brands;
    }

    public List<Brand> selectByCondition(Brand brand) {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = brandMapper.selectByCondition(brand);
        sqlSession.close();
        return brands;
    }

    public Integer batchDel(Integer[] ids) {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        Integer deleted = brandMapper.batchDEl(ids);
        sqlSession.close();
        return deleted;
    }
}
